// 忽略类的概念，用 javascript 的写法实现状态模式

// Finite State Machine
const FSM = {
    off: {
        buttonWasPressed() {
            console.log('关灯');
            this.button.innerHTML = '下一次按我是开灯';
            this.currState = FSM.on;
        }
    },
    on: {
        buttonWasPressed() {
            console.log('开灯');
            this.button.innerHTML = '下一次按我是关灯';
            this.currState = FSM.off;
        }
    }
}

export default class Light {
    constructor() {
        this.currState = FSM.off;
        this.button = null;
    }

    init() {
        let button = document.createElement('button');
        button.style.display = 'block';
        button.innerHTML = '已关灯';
        this.button = document.body.appendChild(button);
        this.button.onclick = () => {
            this.currState.buttonWasPressed.call(this); // 把请求委托给 FSM 状态机
        }
    }
}